/*
 * This file is part of GuitarMaster.
 * 
 * GuitarMaster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GuitarMaster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.paris5.master2.guitarmaster.service;

import com.paris5.master2.guitarmaster.bean.EcomBid;
import com.paris5.master2.guitarmaster.entity.Bid;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.exception.EcomQueryException;
import com.paris5.master2.guitarmaster.exception.EcomValidationException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.*;

/**
 *
 * @author Leticia Mahamed, Alice Khammy, mourad nedjahi, Saliou Mamadou Barry
 */
@Stateless
public class BidTimer implements BidTimerLocal {

    @Resource
    TimerService timerService;
    @EJB
    ServiceAdminRemote serviceAdmin;

    @Override
    public void creerBidTimer(Bid bid) {

        ScheduleExpression schedule = new ScheduleExpression();
        Date dateEndOfBid = bid.getIdProduct().getBidEndDate();
        Calendar cld = new GregorianCalendar();
        cld.setTime(dateEndOfBid);
        schedule.dayOfMonth(cld.get(cld.DAY_OF_MONTH));
        schedule.month(cld.get(cld.MONTH) + 1);
        schedule.year(cld.get(cld.YEAR));
        schedule.hour(cld.get(cld.HOUR_OF_DAY));
        schedule.minute(cld.get(cld.MINUTE));
        schedule.second(cld.get(cld.SECOND));
        TimerConfig config = new TimerConfig(bid, true);
        timerService.createCalendarTimer(schedule, config);

    }

    @Timeout
    public void selBidProduct(Timer timer) throws EcomValidationException, EcomQueryException, EcomException {
        System.out.println("Fin du timer");
        Bid bid = (Bid) timer.getInfo();
        EcomBid eBid = bid.getEcomBid();
        serviceAdmin.endProductToAuction(eBid.getProduct());

    }
}
